package com.adinnet.admin.link.service.impl;

import com.adinnet.admin.link.model.Link;
import com.adinnet.admin.link.mapper.LinkMapper;
import com.adinnet.admin.link.model.vo.LinkReq;
import com.adinnet.admin.link.service.LinkService;
import com.adinnet.admin.order.model.vo.OrderResp;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 意向客户（被分享录入） 服务实现类
 * </p>
 *
 * @author chenjia
 * @since 2020-12-21
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Autowired
    private LinkMapper linkMapper;

    /**
     * @param linkReq:
     * @Description: 意向客户列表
     * @author zYshuai
     * @Date: 2020/12/21 15:42
     * @return: com.adinnet.core.PageResult<com.adinnet.admin.link.model.Link>
     */
    @Override
    public PageResult<Link> contactList(LinkReq linkReq) {
        if (StringUtils.isNotEmpty(linkReq.getSearchDate())) {
            linkReq.setStartDate(linkReq.getSearchDate().substring(0, 10) + " 00:00:00");
            linkReq.setEndDate(linkReq.getSearchDate().substring(13) + " 23:59:59");
        }else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            String now = format.format(new Date());
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, -1);
            Date m = calendar.getTime();
            String mon = format.format(m);
            linkReq.setStartDate(mon);
            linkReq.setEndDate(now);
        }
        if (StringUtils.isNotEmpty(linkReq.getProvinceId())) {
            linkReq.setProvinceId(linkReq.getProvinceId().split("_")[0]);
        }
        if (StringUtils.isNotEmpty(linkReq.getCityId())) {
            linkReq.setCityId(linkReq.getCityId().split("_")[0]);
        }
        if (StringUtils.isNotEmpty(linkReq.getAreaId())) {
            linkReq.setAreaId(linkReq.getAreaId().split("_")[0]);
        }
        Page page = new Page<OrderResp>(linkReq.getPage(), linkReq.getLimit());
        List<Link> linkList = linkMapper.contactList(page, linkReq.getProvinceId(), linkReq.getCityId(), linkReq.getAreaId(), linkReq.getStartDate(), linkReq.getEndDate());
        for (Link link : linkList) {
            link.setProvinceNames((StringUtils.isNotBlank(link.getProvinceName()) ? link.getProvinceName() : "") + (StringUtils.isNotBlank(link.getCityName()) ? link.getCityName() : "") + (StringUtils.isNotBlank(link.getAreaName()) ? link.getAreaName() : ""));
        }
        return new PageResult<Link>(linkList, page.getTotal());
    }
}
